package com.huawei.hianalytics.f.f;

import android.content.Context;
import android.text.TextUtils;
import com.huawei.hianalytics.a.b;
import com.huawei.hianalytics.c.c;
import com.huawei.hianalytics.f.c.a;
import com.huawei.hianalytics.f.g.h;
import com.huawei.hianalytics.f.g.j;
import com.huawei.hianalytics.f.g.k;
import com.huawei.hianalytics.util.f;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;

public class i {
    private static i a;
    private static Map<String, Long> d = new HashMap();
    private Context b;
    private a c = new a();

    public static i a() {
        return d();
    }

    private static void a(String str, Context context, String str2, long j, LinkedHashMap<String, String> linkedHashMap, String str3, String str4) {
        k.a(new d(context, str, str3, k.a(str2, j, (LinkedHashMap) linkedHashMap, str4).toString(), System.currentTimeMillis()));
    }

    private static void a(String str, Context context, String str2, LinkedHashMap<String, String> linkedHashMap) {
        long j = 0;
        long currentTimeMillis = System.currentTimeMillis();
        long longValue = d.containsKey(str) ? ((Long) d.get(str)).longValue() : 0;
        long j2 = currentTimeMillis - longValue;
        if (longValue != 0) {
            j = j2;
        }
        a(str, context, str2, j, linkedHashMap, "$AppOnPause", "OnPause");
    }

    private static synchronized i d() {
        i iVar;
        synchronized (i.class) {
            if (a == null) {
                a = new i();
            }
            iVar = a;
        }
        return iVar;
    }

    public void a(Context context) {
        this.b = context;
        com.huawei.hianalytics.e.a.a().f().c(c.b());
        String b = c.b(context);
        b.d(b);
        if (com.huawei.hianalytics.util.i.a().b()) {
            String a = h.a(context);
            h.a(context, b);
            b.e(a);
            k.a(new m(context));
            return;
        }
        com.huawei.hianalytics.g.b.b("HiAnalyticsEventServer", "userManager.isUserUnlocked() == false");
    }

    public void a(String str, int i) {
        a(str, this.b, j.a(i), b.g());
    }

    public void a(String str, int i, String str2, LinkedHashMap<String, String> linkedHashMap) {
        JSONObject jSONObject = linkedHashMap == null ? new JSONObject() : k.a((LinkedHashMap) linkedHashMap);
        long currentTimeMillis = System.currentTimeMillis();
        if (2 == i) {
            currentTimeMillis = j.a("yyyy-MM-dd", currentTimeMillis);
        }
        k.a(new d(this.b, str, i, str2, jSONObject.toString(), currentTimeMillis));
    }

    public void a(String str, int i, String str2, LinkedHashMap<String, String> linkedHashMap, Map<String, String> map, Map<String, String> map2) {
        JSONObject jSONObject = linkedHashMap == null ? new JSONObject() : k.a((LinkedHashMap) linkedHashMap);
        long currentTimeMillis = System.currentTimeMillis();
        if (2 == i) {
            currentTimeMillis = j.a("yyyy-MM-dd", currentTimeMillis);
        }
        try {
            k.a(new e(this.b, str, i, str2, jSONObject.toString(), currentTimeMillis, map, map2));
        } catch (JSONException e) {
            com.huawei.hianalytics.g.b.c("HiAnalyticsEventServer", "onEventEx() headerEx or commonEx is not json");
        }
    }

    public void a(String str, Context context) {
        if (context == null || this.b == null) {
            com.huawei.hianalytics.g.b.c("HiAnalyticsEventServer", "onPause's mContext is null or SDK was not init.");
            return;
        }
        a(str, this.b, context.getClass().getCanonicalName(), null);
        d.put(str, Long.valueOf(0));
    }

    public void a(String str, Context context, String str2, String str3) {
        if (context == null || this.b == null) {
            com.huawei.hianalytics.g.b.c("HiAnalyticsEventServer", "onReport() null context or SDK was not init.");
            return;
        }
        com.huawei.hianalytics.g.b.b("HiAnalytics/event", "onReport: Before calling runtaskhandler()");
        CharSequence a = f.a(context);
        if (TextUtils.isEmpty(a) || "2G".equals(a)) {
            com.huawei.hianalytics.g.b.c("HiAnalyticsEventServer", "The network is bad.");
        } else {
            k.a(new f(context, str, str2, str3));
        }
    }

    public void a(String str, Context context, LinkedHashMap<String, String> linkedHashMap) {
        if (context == null || this.b == null) {
            com.huawei.hianalytics.g.b.c("HiAnalyticsEventServer", "onPause null context or SDK was not init.");
            return;
        }
        a(str, this.b, context.getClass().getCanonicalName(), (LinkedHashMap) linkedHashMap);
        d.put(str, Long.valueOf(0));
    }

    public void a(String str, String str2) {
        long currentTimeMillis = System.currentTimeMillis();
        long l = com.huawei.hianalytics.a.c.l(str, str2);
        if (l == 0 || currentTimeMillis - l > 30000) {
            com.huawei.hianalytics.g.b.a("HiAnalyticsEventServer", "begin to call onReport!");
            if (TextUtils.isEmpty(str) || !str.equals("_default_config_tag")) {
                com.huawei.hianalytics.a.c.a(currentTimeMillis, str, str2);
            } else {
                com.huawei.hianalytics.a.c.a(currentTimeMillis);
            }
            a(str, this.b, str2, b.g());
            return;
        }
        com.huawei.hianalytics.g.b.c("HiAnalyticsEventServer", "autoReport timeout. interval < 30s ");
    }

    public void a(String str, String str2, String str3) {
        String str4 = "";
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put("_constants", str3);
            String str5 = str;
            String str6 = str2;
            k.a(new d(this.b, str5, str6, jSONObject.toString(), System.currentTimeMillis()));
        } catch (JSONException e) {
            com.huawei.hianalytics.g.b.c("HiAnalyticsEventServer", "onEvent():JSON structure Exception!");
        }
    }

    public void a(String str, String str2, LinkedHashMap<String, String> linkedHashMap) {
        if (this.b == null) {
            com.huawei.hianalytics.g.b.c("HiAnalyticsEventServer", "onPause null context or SDK was not init.");
            return;
        }
        a(str, this.b, str2, (LinkedHashMap) linkedHashMap);
        d.put(str, Long.valueOf(0));
    }

    public void a(boolean z) {
        if (this.b == null) {
            com.huawei.hianalytics.g.b.c("HiAnalyticsEventServer", "onEventCrashInit() SDK was not init.");
        } else if (z) {
            b.a(this.b).a();
        } else {
            b.a(this.b).b();
        }
    }

    public a b() {
        return this.c;
    }

    public void b(String str, int i, String str2, LinkedHashMap<String, String> linkedHashMap) {
        new j(str, j.a(i), str2, (linkedHashMap == null ? new JSONObject() : k.a((LinkedHashMap) linkedHashMap)).toString(), this.b).a();
    }

    public void b(String str, int i, String str2, LinkedHashMap<String, String> linkedHashMap, Map<String, String> map, Map<String, String> map2) {
        try {
            new k(str, j.a(i), str2, (linkedHashMap == null ? new JSONObject() : k.a((LinkedHashMap) linkedHashMap)).toString(), this.b, map, map2).a();
        } catch (JSONException e) {
            com.huawei.hianalytics.g.b.c("HiAnalyticsEventServer", "onStreamEventEx(): headerEx or commonEx is not json");
        }
    }

    public void b(String str, Context context) {
        if (context == null || this.b == null) {
            com.huawei.hianalytics.g.b.c("HiAnalyticsEventServer", "onResume's mContext is null or SDK was not init.");
            return;
        }
        d.put(str, Long.valueOf(System.currentTimeMillis()));
        a(str, this.b, context.getClass().getCanonicalName(), 0, null, "$AppOnResume", "OnResume");
    }

    public void b(String str, Context context, LinkedHashMap<String, String> linkedHashMap) {
        d.put(str, Long.valueOf(System.currentTimeMillis()));
        if (context == null || this.b == null) {
            com.huawei.hianalytics.g.b.c("HiAnalyticsEventServer", "onResume() null context or SDK was not init.");
            return;
        }
        a(str, this.b, context.getClass().getCanonicalName(), 0, linkedHashMap, "$AppOnResume", "OnResume");
    }

    public void b(String str, String str2) {
        if (this.b == null) {
            com.huawei.hianalytics.g.b.c("HiAnalyticsEventServer", "onStartApp() SDK was not init.");
            return;
        }
        JSONObject a = com.huawei.hianalytics.f.g.c.a(this.b, str, str2);
        if (a != null) {
            k.a(new d(this.b, "_instance_ex_tag", "$AppOnStart", a.toString(), System.currentTimeMillis()));
        } else {
            com.huawei.hianalytics.g.b.c("HiAnalyticsEventServer", "onStartApp() getInfoJson is null,The end of the event ");
        }
    }

    public void b(String str, String str2, LinkedHashMap<String, String> linkedHashMap) {
        d.put(str, Long.valueOf(System.currentTimeMillis()));
        a(str, this.b, str2, 0, linkedHashMap, "$AppOnResume", "OnResume");
    }

    public void c() {
        if (this.b == null) {
            com.huawei.hianalytics.g.b.c("HiAnalyticsEventServer", "onFirstRun() SDK was not init.");
            return;
        }
        JSONObject a = com.huawei.hianalytics.f.g.c.a(this.b);
        if (a != null) {
            k.a(new d(this.b, "_instance_ex_tag", "$AppFirstStart", a.toString(), System.currentTimeMillis()));
        } else {
            com.huawei.hianalytics.g.b.c("HiAnalyticsEventServer", "onFirstRun() getInfoJson is null,The end of the event ");
        }
    }

    public void c(String str, String str2) {
        if (this.b == null) {
            com.huawei.hianalytics.g.b.c("HiAnalyticsEventServer", "onAppUpgrade() SDK was not init.");
            return;
        }
        JSONObject a = com.huawei.hianalytics.f.g.c.a(str, str2);
        if (a != null) {
            k.a(new d(this.b, "_instance_ex_tag", "$AppOnUpdate", a.toString(), System.currentTimeMillis()));
        } else {
            com.huawei.hianalytics.g.b.c("HiAnalyticsEventServer", "onAppUpgrade() getInfoJson is null,The end of the event ");
        }
    }
}
